//
//  MBForumMainCell.m
//  MiBaoTu
//
//  Created by admin on 16/10/14.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBForumMainCell.h"

@interface MBForumMainCell () {
    UIView                  *_bgView;
    //头部
    UIView                  *_topView;
    UIImageView             *_userImgView;
    UILabel                 *_nickNameLb;
    UILabel                 *_timeLb;
    UIButton                *_deleteBt;
    UIImageView             *_topImgView;
    UIView                  *_lineView;
    UIImageView             *_essenceImg;//精华标志
    UIImageView             *_topImg;//置顶
    //内容
    UILabel                 *_contentTitleLb;  //标题
    UILabel                 *_contentShowLb;    //内容
    
    //图片
    UIView                  *_picView;
    UIImageView             *_imgView1;
    UIImageView             *_imgView2;
    UIImageView             *_imgView3;
    UIButton                *_picBt;
    NSMutableArray          *imgContentArr;
    
    //尾部
    UIView                  *_bottomView;
    UILabel                 *_zanLb;
    UIImageView             *_zanImgView;
    UIButton                *_zanBt;
    UILabel                 *_pinglunLb;
    UIImageView             *_pinglunImgView;
    UIButton                *_pinglunBt;
    UILabel                 *_guankanLb;
    UIImageView             *_guankanImgView;
    UIButton                *_guankanlunBt;
}

@end
@implementation MBForumMainCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    /*******************************头部*******************/
    _topView = [[UIView alloc]initWithFrame:CGRectZero];
    _topView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:_topView];
    //头像
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _userImgView.layer.cornerRadius = 40*PERCEN;
    _userImgView.layer.masksToBounds = YES;
    _userImgView.backgroundColor = [UIColor clearColor];
    _userImgView.contentMode = UIViewContentModeScaleAspectFit;
    [_topView addSubview:_userImgView];
    
    //昵称
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:14.f];
    
    if (ISIPAD) {
        _nickNameLb.font = [UIFont systemFontOfSize:30*PERCEN];

    }
    [_topView addSubview:_nickNameLb];
    
    //时间
    _timeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _timeLb.backgroundColor = [UIColor clearColor];
    _timeLb.textAlignment = NSTextAlignmentLeft;
    _timeLb.textColor = UIColorFromHex(0x999999);
    _timeLb.font = [UIFont systemFontOfSize:12.f];
    
    if (ISIPAD) {
        _timeLb.font = [UIFont systemFontOfSize:26*PERCEN];
        
    }
    [_topView addSubview:_timeLb];
    
    //删除
    _deleteBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteBt.backgroundColor = [UIColor clearColor];
    [_deleteBt setImage:[UIImage imageNamed:@"shanchu2"] forState:UIControlStateNormal];
    [_deleteBt setImage:[UIImage imageNamed:@"shanchu2"] forState:UIControlStateHighlighted];
    [_deleteBt addTarget:self action:@selector(onSelectForDelete) forControlEvents:UIControlEventTouchUpInside];
    _deleteBt.hidden = YES;
    [_topView addSubview:_deleteBt];

    //置顶
    _topImg = [[UIImageView alloc]initWithFrame:CGRectZero];
    _topImg.backgroundColor = [UIColor clearColor];
    _topImg.image = [UIImage imageNamed:@"zhiding"];
    _topImg.hidden = YES;
    [_topView addSubview:_topImg];
    
    //精华
    _essenceImg = [[UIImageView alloc]initWithFrame:CGRectZero];
    _essenceImg.backgroundColor = [UIColor clearColor];
    _essenceImg.image = [UIImage imageNamed:@"jing"];
    _essenceImg.hidden = YES;
    [_topView addSubview:_essenceImg];
    
    if (ISIPAD) {
        _topImg.image=[UIImage imageNamed:@"zhidingI"];
        _essenceImg.image=[UIImage imageNamed:@"jingI"];
    }
    //官方标志
    _topImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _topImgView.backgroundColor = [UIColor clearColor];
    _topImgView.image = [UIImage imageNamed:@"guanfang"];
    _topImgView.hidden = YES;
    [_topView addSubview:_topImgView];
    
    //线
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColorFromHex(0x999999)colorWithAlphaComponent:.15];
    [_topView addSubview:_lineView];
    
    /*******************************内容*******************/
    //内容标题
    _contentTitleLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _contentTitleLb.backgroundColor = [UIColor clearColor];
    _contentTitleLb.textAlignment = NSTextAlignmentLeft;
    _contentTitleLb.textColor = UIColorFromHex(0x333333);
    _contentTitleLb.font = [UIFont systemFontOfSize:15.f];
    if (ISIPAD) {
         _contentTitleLb.font = [UIFont systemFontOfSize:32*PERCEN];
    }
    [_bgView addSubview:_contentTitleLb];
    
    //内容
    _contentShowLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _contentShowLb.backgroundColor = [UIColor clearColor];
    _contentShowLb.textAlignment = NSTextAlignmentLeft;
    _contentShowLb.textColor = UIColorFromHex(0x333333);
    
    _contentShowLb.font = [UIFont systemFontOfSize:13.f];
    if (ISIPAD) {
         _contentShowLb.font = [UIFont systemFontOfSize:28*PERCEN];
    }
    _contentShowLb.numberOfLines = 3;
    [_bgView addSubview:_contentShowLb];
    
    //图片
    _picView = [[UIView alloc]initWithFrame:CGRectZero];
    _picView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:_picView];
    
    _imgView1 = [[UIImageView alloc]initWithFrame:CGRectZero];
    _imgView1.backgroundColor = [UIColor clearColor];
    _imgView1.layer.cornerRadius = 5.f;
    _imgView1.layer.masksToBounds = YES;
    _imgView1.tag=0;
    [_picView addSubview:_imgView1];
    
    _imgView2 = [[UIImageView alloc]initWithFrame:CGRectZero];
    _imgView2.backgroundColor = [UIColor clearColor];
    _imgView2.layer.cornerRadius = 5.f;
    _imgView2.layer.masksToBounds = YES;
    _imgView2.tag=1;
    [_picView addSubview:_imgView2];
    
    _imgView3 = [[UIImageView alloc]initWithFrame:CGRectZero];
    _imgView3.backgroundColor = [UIColor clearColor];
    _imgView3.layer.cornerRadius = 5.f;
    _imgView3.layer.masksToBounds = YES;
    _imgView3.tag=2;
    [_picView addSubview:_imgView3];
    
    _picBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _picBt.backgroundColor = [UIColor clearColor];
    [_picBt addTarget:self action:@selector(onSelectForSeePicture) forControlEvents:UIControlEventTouchUpInside];
   // [_picView addSubview:_picBt];
    _imgView1.userInteractionEnabled=YES;
    _imgView2.userInteractionEnabled=YES;
    _imgView3.userInteractionEnabled=YES;
    
    UITapGestureRecognizer*tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onSelectForSeePicture:)];
     UITapGestureRecognizer*tap2=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onSelectForSeePicture:)];
     UITapGestureRecognizer*tap3=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onSelectForSeePicture:)];
    [_imgView1 addGestureRecognizer:tap1];
    [_imgView2 addGestureRecognizer:tap2];
    [_imgView3 addGestureRecognizer:tap3];
     /*******************************尾部*******************/
    _bottomView = [[UIView alloc]initWithFrame:CGRectZero];
    _bottomView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:_bottomView];
    
    //赞
    _zanLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _zanLb.backgroundColor = [UIColor clearColor];
    _zanLb.textAlignment = NSTextAlignmentRight;
    _zanLb.textColor = UIColorFromHex(0x999999);
    _zanLb.font = [UIFont systemFontOfSize:13.f];
    if (ISIPAD) {
         _zanLb.font = [UIFont systemFontOfSize:26*PERCEN];
    }
    [_bottomView addSubview:_zanLb];
    
    _zanImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _zanImgView.backgroundColor = [UIColor clearColor];
    [_bottomView addSubview:_zanImgView];
    
    _zanBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _zanBt.backgroundColor = [UIColor clearColor];
    [_zanBt addTarget:self action:@selector(onSelectForZan:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_zanBt];
    
    //评论
    _pinglunLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _pinglunLb.backgroundColor = [UIColor clearColor];
    _pinglunLb.textAlignment = NSTextAlignmentRight;
    _pinglunLb.textColor = UIColorFromHex(0x999999);
    _pinglunLb.font = [UIFont systemFontOfSize:13.f];
    if (ISIPAD) {
        _pinglunLb.font = [UIFont systemFontOfSize:26*PERCEN];
    }
    [_bottomView addSubview:_pinglunLb];
    
    _pinglunImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _pinglunImgView.backgroundColor = [UIColor clearColor];
    [_bottomView addSubview:_pinglunImgView];
    
    _pinglunBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _pinglunBt.backgroundColor = [UIColor clearColor];
    [_bottomView addSubview:_pinglunBt];
    
    //观看
    _guankanLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _guankanLb.backgroundColor = [UIColor clearColor];
    _guankanLb.textAlignment = NSTextAlignmentRight;
    _guankanLb.textColor = UIColorFromHex(0x999999);
    _guankanLb.font = [UIFont systemFontOfSize:13.f];
    if (ISIPAD) {
        _guankanLb.font = [UIFont systemFontOfSize:26*PERCEN];
    }
    [_bottomView addSubview:_guankanLb];
    
    _guankanImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _guankanImgView.backgroundColor = [UIColor clearColor];
    [_bottomView addSubview:_guankanImgView];

    _guankanlunBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _guankanlunBt.backgroundColor = [UIColor clearColor];
    [_bottomView addSubview:_guankanlunBt];
    
    imgContentArr = [[NSMutableArray alloc]init];
}

#pragma mark - Actions
//删除
- (void)onSelectForDelete
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForDeleteByForumCellWithView:withValue:)]) {
        [_delegate onSelectItemForDeleteByForumCellWithView:self withValue:_dataDic];
    }
}

//查看图片
- (void)onSelectForSeePicture:(UITapGestureRecognizer*)tap
{
    NSInteger tag=tap.view.tag;
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForSeePictureByForumCellWithView:withValue:withNum:)]) {
        [_delegate onSelectItemForSeePictureByForumCellWithView:self withValue:imgContentArr withNum:tag];
    }
}

- (void)onSelectForZan:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForZanByForumCellWithView:withValue:)]) {
       
        [_delegate onSelectItemForZanByForumCellWithView:self withValue:_dataDic];
    }
}

//修改点赞按钮的选择
- (void)onUpdateZanBtAction
{
    _zanBt.selected = !_zanBt.selected;
    if (_zanBt.selected) {
        _zanImgView.image = [UIImage imageNamed:@"zan2"];
    } else {
        _zanImgView.image = [UIImage imageNamed:@"zan1"];
    }
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    if (!_dataDic) {
        _dataDic = [[NSDictionary alloc]init];
    }
    _dataDic = dataDic;
    
    [self setNeedsLayout];
}

- (void)setIsParise:(BOOL)isParise
{
    _isParise = isParise;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //value
    if (_dataDic.allKeys.count == 0) return;
    
    
    NSString*EliteStatus=[NSString stringWithFormat:@"%@",_dataDic[@"EliteStatus"]]   ;
    NSString*TopStatus= [NSString stringWithFormat:@"%@",_dataDic[@"TopStatus"]];
    

    
    //top
    [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",MUSIC_PLAY_Url,[_dataDic objectForKey:@"FaceImg"]]] placeholderImage:[UIImage imageNamed:@"xiaobian_bg"]];
    _nickNameLb.text = [_dataDic objectForKey:@"NickName"];
    _timeLb.text = [[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"CreateTime"]] getChaTime];
    NSString *UserID = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"UserID"]];
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model) {
        _deleteBt.hidden = YES;
    }
    
     if (_isNew) {
        if ([EliteStatus isEqualToString:@"1"]&&[TopStatus isEqualToString:@"1"]) {
              _essenceImg.hidden=NO;
            _topImg.hidden=NO;
            _deleteBt.hidden=YES;
            
                        _contentShowLb.hidden=YES;
                        _picView.hidden=YES;
                        _bottomView.hidden=YES;
            
        }
           else if ([EliteStatus isEqualToString:@"1"]&&![TopStatus isEqualToString:@"1"]) {
                _essenceImg.hidden=NO;
                _deleteBt.hidden=YES;
                _topImg.hidden=YES;
               
               _contentShowLb.hidden=YES;
               _picView.hidden=YES;
               _bottomView.hidden=YES;

            }
            else if ([TopStatus isEqualToString:@"1"]&&![EliteStatus isEqualToString:@"1"]) {
                _topImg.hidden=NO;
                _deleteBt.hidden=YES;
                 _essenceImg.hidden=YES;
                
                _contentShowLb.hidden=YES;
                _picView.hidden=YES;
                _bottomView.hidden=YES;

            }else {
                _essenceImg.hidden=YES;
                _topImg.hidden=YES;
                
                _contentShowLb.hidden=NO;
                _picView.hidden=NO;
                _bottomView.hidden=NO;

            }
        }else{
            _essenceImg.hidden=YES;
            _topImg.hidden=YES;
            _contentShowLb.hidden=NO;
            _picView.hidden=NO;
            _bottomView.hidden=NO;
        }
    

        if ([UserID isEqualToString:[NSString stringWithFormat:@"%@",model.UserID]]) {
        _deleteBt.hidden = NO;
    } else {
        _deleteBt.hidden = YES;
    }
    
    if (_isActivity) {
        _topImgView.hidden = NO;
        _nickNameLb.text = (_nickNameLb.text.length == 0 ? @"米宝兔官方帐号" : _nickNameLb.text);
    } else {
        _topImgView.hidden = YES;
        
    }
       //content
    _contentTitleLb.text = [_dataDic objectForKey:@"Title"];
    NSString *content = [_dataDic objectForKey:@"Content"];//@"今天和Emily一起去海洋馆玩了，很累，不过很开心！非常值得，期待下次的亲子游记。今天和Emily一起去海洋馆玩了，很累，不过很开心！非常值得，期待下次的亲子游记。今天和Emily一起去海洋馆玩了，很累，不过很开心！非常值得，期待下次的亲子游记。今天和Emily一起去海洋馆玩了，很累，不过很开心！非常值得，期待下次的亲子游记。";
    
    
    
    NSMutableString *contMutStr = [[NSMutableString alloc]init];
    if (_isActivity == YES) {
        [imgContentArr removeAllObjects];
        
        NSMutableString *showContent = [NSMutableString stringWithString:content];
        [showContent replaceOccurrencesOfString:@"<p>" withString:@"" options:1 range:NSMakeRange(0, showContent.length)];
        [showContent replaceOccurrencesOfString:@"</p>" withString:@"\n" options:1 range:NSMakeRange(0, showContent.length)];
        [showContent replaceOccurrencesOfString:@"<br/>" withString:@"\n" options:1 range:NSMakeRange(0, showContent.length)];
        //包含p
        NSMutableString *nShowContent = [[NSMutableString alloc]init];
        if ([showContent rangeOfString:@"<p "].location != NSNotFound) {
            NSArray *demoArr = [showContent componentsSeparatedByString:@">"];
            for (NSString *string in demoArr) {
                if ([string containsString:@"<p "]) {
                    NSArray *stringArr = [string componentsSeparatedByString:@"<p "];
                    [nShowContent appendString:stringArr[0]];
                } else {
                    [nShowContent appendString:string];
                }
            }
        } else {
            nShowContent = showContent;
        }
        
        NSArray *contentArr = [nShowContent componentsSeparatedByString:@"<img src="];
        for (NSString *picStr in contentArr) {
            if ([picStr containsString:@" title="]) {
                //纯图片
                NSArray *picUrlArr = [picStr componentsSeparatedByString:@" title="];
                NSMutableString *picUrlStr = [NSMutableString stringWithString:picUrlArr[0]];
                if (picUrlStr.length > 0) {
                    [picUrlStr replaceOccurrencesOfString:@"\"" withString:@"" options:1 range:NSMakeRange(0, picUrlStr.length)];
                    [picUrlStr insertString:[NSString stringWithFormat:@"%@/",MUSIC_PLAY_Url] atIndex:0];
                    [imgContentArr addObject:picUrlStr];
                }
                if ([picUrlStr containsString:@"\"/>"]) {
                    NSArray *picContentArr = [picUrlStr componentsSeparatedByString:@"\"/>"];
                    [contMutStr appendString:picContentArr[1]];
                }
            } else {
                [contMutStr appendString:picStr];
            }
        }
        

    } else {
        
        NSArray *contentArr = [content componentsSeparatedByString:@"\n"];
        [imgContentArr removeAllObjects];
        if (contentArr.count == 1) {
            contentArr = [content componentsSeparatedByString:@"<img src ="];
            for (NSString *showContent in contentArr) {
                if (![showContent containsString:@"width=\"100%\">"]) {
                    NSArray *ntextAllArr = [showContent componentsSeparatedByString:@"width=\"100%\">"];
                    for (NSString *tt in ntextAllArr) {
                        if ([tt rangeOfString:@".png"].location != NSNotFound ||
                            [tt rangeOfString:@".PNG"].location != NSNotFound ||
                            [tt rangeOfString:@".jpg"].location != NSNotFound ||
                            [tt rangeOfString:@".JPG"].location != NSNotFound ||
                            [tt rangeOfString:@".jpeg"].location != NSNotFound ||
                            [tt rangeOfString:@".JPEG"].location != NSNotFound) {
                            //该结为图片
                            NSMutableString *imgStr = [NSMutableString stringWithFormat:@"%@",tt];
                            [imgStr replaceOccurrencesOfString:@" '/" withString:@"" options:1 range:NSMakeRange(0, imgStr.length)];
                            [imgStr replaceOccurrencesOfString:@"'/ " withString:@"" options:1 range:NSMakeRange(0, imgStr.length)];
                            [imgStr insertString:[NSString stringWithFormat:@"%@/",MUSIC_PLAY_Url] atIndex:0];
                            [imgContentArr addObject:imgStr];
                        } else {
                            [contMutStr appendString:tt];
                        }
                    }
                } else {
                    NSArray *nArr = [showContent componentsSeparatedByString:@"width=\"100%\">"];
                    for (NSString *tt in nArr) {
                        if ([tt rangeOfString:@".png"].location != NSNotFound ||
                            [tt rangeOfString:@".PNG"].location != NSNotFound ||
                            [tt rangeOfString:@".jpg"].location != NSNotFound ||
                            [tt rangeOfString:@".JPG"].location != NSNotFound ||
                            [tt rangeOfString:@".jpeg"].location != NSNotFound ||
                            [tt rangeOfString:@".JPEG"].location != NSNotFound) {
                            //该结为图片
                            NSMutableString *imgStr = [NSMutableString stringWithFormat:@"%@",tt];
                            [imgStr replaceOccurrencesOfString:@" '/" withString:@"" options:1 range:NSMakeRange(0, imgStr.length)];
                            [imgStr replaceOccurrencesOfString:@"'/ " withString:@"" options:1 range:NSMakeRange(0, imgStr.length)];
                            [imgStr insertString:[NSString stringWithFormat:@"%@/",MUSIC_PLAY_Url] atIndex:0];
                            [imgContentArr addObject:imgStr];
                        } else {
                            [contMutStr appendString:tt];
                        }
                    }
                }
            }
        }
        else {
            for (NSString *showContent in contentArr) {
                if (![showContent hasPrefix:@"<img src ="]) {
                    if ([showContent rangeOfString:@"<img src ="].location != NSNotFound) {
                        //这段蚊子中包含图片地址，拆解吧 愤怒的少年
                        NSArray *contentAllArr = [showContent componentsSeparatedByString:@"<img src ="];
                        for (NSString *ntext in contentAllArr) {
                            if ([ntext rangeOfString:@"width=\"100%\">"].location != NSNotFound) {
                                //查看这节是否是图片地址部分
                                NSArray *ntextAllArr = [ntext componentsSeparatedByString:@"width=\"100%\">"];
                                for (NSString *tt in ntextAllArr) {
                                    if ([tt rangeOfString:@".png"].location != NSNotFound ||
                                        [tt rangeOfString:@".PNG"].location != NSNotFound ||
                                        [tt rangeOfString:@".jpg"].location != NSNotFound ||
                                        [tt rangeOfString:@".JPG"].location != NSNotFound ||
                                        [tt rangeOfString:@".jpeg"].location != NSNotFound ||
                                        [tt rangeOfString:@".JPEG"].location != NSNotFound) {
                                        //该结为图片
                                        NSMutableString *imgStr = [NSMutableString stringWithFormat:@"%@",tt];
                                        [imgStr replaceOccurrencesOfString:@" '/" withString:@"" options:1 range:NSMakeRange(0, imgStr.length)];
                                        [imgStr replaceOccurrencesOfString:@"'/ " withString:@"" options:1 range:NSMakeRange(0, imgStr.length)];
                                        [imgStr insertString:[NSString stringWithFormat:@"%@/",MUSIC_PLAY_Url] atIndex:0];
                                        [imgContentArr addObject:imgStr];
                                    } else {
                                        [contMutStr appendString:tt];
                                    }
                                }
                            } else {
                                [contMutStr appendString:ntext];
                            }
                        }
                    } else {
                        [contMutStr appendString:showContent];
                    }
                } else {
                    //添加图片
                    NSMutableString *imgStr = [NSMutableString stringWithFormat:@"%@",showContent];
                    [imgStr replaceOccurrencesOfString:@"<img src = " withString:@"" options:1 range:NSMakeRange(0, imgStr.length)];
                    [imgStr replaceOccurrencesOfString:@"'/" withString:@"" options:1 range:NSMakeRange(0, imgStr.length)];
                    [imgStr replaceOccurrencesOfString:@" width=\"100%\">" withString:@"" options:1 range:NSMakeRange(0, imgStr.length)];
                    [imgStr insertString:[NSString stringWithFormat:@"%@/",MUSIC_PLAY_Url] atIndex:0];
                    [imgContentArr addObject:imgStr];
                }
            }
        }
    }
    _contentShowLb.text = contMutStr;
    if (imgContentArr.count >= 3) {
        [_imgView1 sd_setImageWithURL:[NSURL URLWithString:imgContentArr[0]] placeholderImage:[UIImage imageNamed:@"fourm_bg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self onUpdateUIForPicture:image withImgView:_imgView1];
        }];
        [_imgView2 sd_setImageWithURL:[NSURL URLWithString:imgContentArr[1]] placeholderImage:[UIImage imageNamed:@"fourm_bg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self onUpdateUIForPicture:image withImgView:_imgView2];
        }];
        [_imgView3 sd_setImageWithURL:[NSURL URLWithString:imgContentArr[2]] placeholderImage:[UIImage imageNamed:@"fourm_bg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self onUpdateUIForPicture:image withImgView:_imgView3];
        }];
        
        _imgView1.hidden = NO;
        _imgView2.hidden = NO;
        _imgView3.hidden = NO;
    }
    else if (imgContentArr.count >= 2 ) {
        [_imgView1 sd_setImageWithURL:[NSURL URLWithString:imgContentArr[0]] placeholderImage:[UIImage imageNamed:@"fourm_bg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self onUpdateUIForPicture:image withImgView:_imgView1];
        }];
        [_imgView2 sd_setImageWithURL:[NSURL URLWithString:imgContentArr[1]] placeholderImage:[UIImage imageNamed:@"fourm_bg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self onUpdateUIForPicture:image withImgView:_imgView2];
        }];
        
        _imgView1.hidden = NO;
        _imgView2.hidden = NO;
        _imgView3.hidden = YES;
    }
    else if (imgContentArr.count >= 1) {
        [_imgView1 sd_setImageWithURL:[NSURL URLWithString:imgContentArr[0]] placeholderImage:[UIImage imageNamed:@"fourm_bg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self onUpdateUIForPicture:image withImgView:_imgView1];
        }];
        
        _imgView1.hidden = NO;
        _imgView2.hidden = YES;
        _imgView3.hidden = YES;
    }
    else if (imgContentArr.count == 0) {
        _imgView1.hidden = YES;
        _imgView2.hidden = YES;
        _imgView3.hidden = YES;
    }
    
    //bottom
    _zanLb.text = [[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"PraiseNum"]]getValues];
    _zanBt.selected = _isParise;
    if (_zanBt.selected) {
        _zanImgView.image = [UIImage imageNamed:@"zan2"];
        _zanLb.textColor = TINK_COLOR;
    } else {
        _zanImgView.image = [UIImage imageNamed:@"zan1"];
        _zanLb.textColor = UIColorFromHex(0x999999);
    }

    _pinglunLb.text = [[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"CommentNum"]]getValues];
    _guankanLb.text = [[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"ViewNum"]]getValues];
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 600*PERCEN);
    
    //top 120*percen
    _topView.frame = CGRectMake(0, 0, _bgView.width, (ISIPAD?140*PERCEN:120*PERCEN));
    _userImgView.frame = CGRectMake(25*PERCEN, (_topView.height-80*PERCEN)/2., 80*PERCEN, 80*PERCEN);
    if (ISIPAD) {
        _userImgView.frame = CGRectMake(460*PERCEN, 20*PERCEN  , 100*PERCEN, 100*PERCEN);
    }
    _nickNameLb.frame = CGRectMake(_userImgView.right+20*PERCEN, _userImgView.top, 140, (ISIPAD?30*PERCEN:20));
    _timeLb.frame = CGRectMake(_nickNameLb.left, _userImgView.bottom-20, _nickNameLb.width, 20);
    if (ISIPAD) {
         _timeLb.frame = CGRectMake(_nickNameLb.left, _nickNameLb.bottom+30*PERCEN  , _nickNameLb.width, 26*PERCEN);
    }
    UIImage *deleteImg = [UIImage imageNamed:@"shanchu"];
    _deleteBt.frame = CGRectMake(_topView.width-25*PERCEN-deleteImg.size.width, 30*PERCEN, deleteImg.size.width, deleteImg.size.height);
    if (ISIPAD) {
         _deleteBt.frame = CGRectMake(_topView.width-470*PERCEN-deleteImg.size.width, _nickNameLb.top, deleteImg.size.width, deleteImg.size.height);
    }
    
    
    UIImage*zhidingImg=[UIImage imageNamed:@"zhiding"];
    UIImage*zhidingImg1=[UIImage imageNamed:@"zhidingI"];
    _topImg.frame=CGRectMake(_topView.width-30*PERCEN-zhidingImg.size.width, 30*PERCEN, zhidingImg.size.width, zhidingImg.size.height);
    if (ISIPAD) {
        _topImg.frame = CGRectMake(_topView.width-470*PERCEN-zhidingImg1.size.width, 30*PERCEN, zhidingImg1.size.width, zhidingImg1.size.height);
    }
    
    UIImage*jinghuaImg=[UIImage imageNamed:@"jing"];
    UIImage*jinghuaImg1=[UIImage imageNamed:@"jingI"];
    if (_topImg.hidden==YES) {
        if (ISIPAD) {
            _essenceImg.frame=CGRectMake(_topView.width-470*PERCEN-jinghuaImg1.size.width, 30*PERCEN, jinghuaImg1.size.width, jinghuaImg1.size.height);
        }else{
              _essenceImg.frame=CGRectMake(_topView.width-30*PERCEN-jinghuaImg.size.width, 30*PERCEN, jinghuaImg.size.width, jinghuaImg.size.height);
        }
       
    }else{
        if (ISIPAD) {
              _essenceImg.frame=CGRectMake(_topImg.left-20*PERCEN-jinghuaImg1.size.width, 30*PERCEN, jinghuaImg1.size.width, jinghuaImg1.size.height);
        }else{
        
    _essenceImg.frame=CGRectMake(_topImg.left-20*PERCEN-jinghuaImg.size.width, 30*PERCEN, jinghuaImg.size.width, jinghuaImg.size.height);
        }
    }
    
    
    UIImage *topImg = [UIImage imageNamed:@"guanfang"];
    _topImgView.frame = CGRectMake(_deleteBt.left, _deleteBt.top, topImg.size.width, topImg.size.height);
    _lineView.frame = CGRectMake(_userImgView.left, 119*PERCEN, _topView.width-25*PERCEN, 1);
    
    if (ISIPAD) {
        _lineView.frame = CGRectMake(440*PERCEN,_userImgView.bottom+19*PERCEN  , _topView.width-880*PERCEN, 1);
    }
    
    
    //content 20+
      _contentTitleLb.frame = CGRectMake(_userImgView.left, _topView.bottom+20*PERCEN, 200, 20);
    
    if ([EliteStatus isEqualToString:@"0"]&&[TopStatus isEqualToString:@"0"]) {
        
        _contentShowLb.frame = CGRectMake(_contentTitleLb.left, _contentTitleLb.bottom, _topView.width-2*25*PERCEN, 100);
        if (ISIPAD) {
            _contentTitleLb.frame = CGRectMake(_userImgView.left+10*PERCEN, _topView.bottom+30*PERCEN, 400*PERCEN, 32*PERCEN);
            
            _contentShowLb.frame = CGRectMake(_contentTitleLb.left, _contentTitleLb.bottom+20*PERCEN, _topView.width-2*470*PERCEN, 100);
        }
        
        
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
        paragraph.lineSpacing = 10*PERCEN;
        NSMutableAttributedString *mutStr = [[NSMutableAttributedString alloc]initWithString:contMutStr];
        [mutStr addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, contMutStr.length)];
        if (ISIPAD) {
            [mutStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:28*PERCEN] range:NSMakeRange(0, contMutStr.length)];
        }else{
            [mutStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.f] range:NSMakeRange(0, contMutStr.length)];
        }
        
        float contentHeight =[mutStr boundingRectWithSize:CGSizeMake(_contentShowLb.width, 20000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:NULL].size.height;
        
        _contentShowLb.height = contentHeight < 110*PERCEN?contentHeight:110*PERCEN;
        
        //picture
        if (ISIPAD) {
            _picView.frame = CGRectMake(_userImgView.left+10*PERCEN, _contentShowLb.bottom+(_imgView1.isHidden==YES?0:30*PERCEN), _contentShowLb.width, (_imgView1.isHidden == YES ? 0 : 352*PERCEN));
            _imgView1.frame = CGRectMake(0, 0, 352*PERCEN, 352*PERCEN);
            _imgView2.frame = CGRectMake(_imgView1.right+26*PERCEN, 0, 352*PERCEN, 352*PERCEN);
            _imgView3.frame = CGRectMake(_imgView2.right+26*PERCEN, 0, 352*PERCEN, 352*PERCEN);
            _picBt.frame = _picView.bounds;
            
        }else{
            _picView.frame = CGRectMake(_userImgView.left, _contentShowLb.bottom+(_imgView1.isHidden==YES?0:10*PERCEN), _contentShowLb.width, (_imgView1.isHidden == YES ? 0 : 220*PERCEN));
            _imgView1.frame = CGRectMake(0, 0, 220*PERCEN, 220*PERCEN);
            _imgView2.frame = CGRectMake(_imgView1.right+20*PERCEN, 0, 220*PERCEN, 220*PERCEN);
            _imgView3.frame = CGRectMake(_imgView2.right+20*PERCEN, 0, 220*PERCEN, 220*PERCEN);
            _picBt.frame = _picView.bounds;
        }
        //bottom
        if (ISIPAD) {
            _bottomView.frame = CGRectMake(_picView.left, _picView.bottom+30*PERCEN,_bgView.width-2*460*PERCEN, 40*PERCEN);
            
        }else{
            _bottomView.frame = CGRectMake(25*PERCEN, _picView.bottom+20*PERCEN, _topView.width-2*25*PERCEN, 40*PERCEN);
        }
        
        float zanWidth =[_zanLb.text boundingRectWithSize:CGSizeMake(1000, _bottomView.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_zanLb.font} context:NULL].size.width+10*PERCEN;
        _zanLb.frame = CGRectMake(_bottomView.width-zanWidth, 0, zanWidth, _bottomView.height);
        
        _zanImgView.frame = CGRectMake(_zanLb.left-_zanImgView.image.size.width, (_bottomView.height-_zanImgView.image.size.height)/2., _zanImgView.image.size.width, _zanImgView.image.size.height);
        _zanBt.frame = CGRectMake(_zanImgView.left, _zanLb.top, _zanLb.right-_zanImgView.left, _zanLb.height);
        
        float pinlunWidth =[_pinglunLb.text boundingRectWithSize:CGSizeMake(1000, _bottomView.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_pinglunLb.font} context:NULL].size.width+10*PERCEN;
        _pinglunLb.frame = CGRectMake(_zanImgView.left-30*PERCEN-zanWidth, 0, pinlunWidth, _bottomView.height);
        _pinglunImgView.image = [UIImage imageNamed:@"pinglun"];
        _pinglunImgView.frame = CGRectMake(_pinglunLb.left-_pinglunImgView.image.size.width, (_bottomView.height-_pinglunImgView.image.size.height)/2., _pinglunImgView.image.size.width, _pinglunImgView.image.size.height);
        
        float guankanWidth =[_guankanLb.text boundingRectWithSize:CGSizeMake(1000, _bottomView.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_guankanLb.font} context:NULL].size.width+10*PERCEN;
        _guankanLb.frame = CGRectMake(_pinglunImgView.left-30*PERCEN-guankanWidth, 0, guankanWidth, _bottomView.height);
        _guankanImgView.image = [UIImage imageNamed:@"liulan"];
        _guankanImgView.frame = CGRectMake(_guankanLb.left-_guankanImgView.image.size.width, (_bottomView.height-_guankanImgView.image.size.height)/2., _guankanImgView.image.size.width, _guankanImgView.image.size.height);
        
        _bgView.height = _bottomView.bottom+20*PERCEN;
    }else{
        
        
        if (ISIPAD) {
            _bgView.height=222*PERCEN;
        }else{
            _bgView.height=190*PERCEN;
        }

      
       
    }
   
}

- (void)onUpdateUIForPicture:(UIImage *)image withImgView:(UIImageView *)imgView
{
    CGImageRef imageRef = image.CGImage;
    float imgHeight = 1.0 * image.size.width * imgView.height/imgView.width;
    float rectHeight = imgHeight;
    float top = (image.size.height-imgHeight)/2.;
    CGRect rect;
    if (top > 0) {
        //图片实际高度比UI高度高
        if (imgHeight > image.size.height) {
            rectHeight = image.size.height;
            top = (imgHeight - image.size.height)/2.;
        }
        rect = CGRectMake(0, top, image.size.width, rectHeight);
    }
    else {
        //UI高度比图片实际高度高
        float imgWidth = 1.0 * image.size.height * imgView.width/imgView.height;
        float rectWidth = imgWidth;
        float left = (image.size.width-imgWidth)/2.;
        if (imgWidth > image.size.width) {
            rectWidth = image.size.width;
            left = (imgWidth - image.size.width)/2.;
        }
        rect = CGRectMake(left, 0, rectWidth, image.size.height);
    }
    CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *rectImg = [UIImage imageWithCGImage:imagePartRef];
    CGImageRelease(imagePartRef);
    
    imgView.image = rectImg;
}

@end
